package com.mg.g2048.SD;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.mg.g2048.Data.AppConfig;
public class DBHelper extends SQLiteOpenHelper { 
	private static String DATABASE_NAME = "score.db"; 
	private final static int DATABASE_VERSION = 1; 
	Context context = null;
	// 以下是最高分的信息
	private final static String TABLE_NAME = "score_table"; 
	public final static String SCORE_MAX = "score_max"; 
	public final static String SCORE_SOUND_OPEN = "score_sound_open"; 
	SQLiteDatabase db = null;
	
	static {
		DATABASE_NAME = SDHelper.getSDPath() + "/" + AppConfig.Path.PARENT + "/score.db";
	}
	
	public DBHelper(Context context) { 
		super(context, DATABASE_NAME, null, DATABASE_VERSION); //
		this.context = context;
	} 
	
	//创建table 
	@Override 
	public void onCreate(SQLiteDatabase db) { 
		// 最高分
		creatMaxScoreInfo(db);
	} 
	
	@Override 
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; 
		try{
			db.execSQL(sql); 
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"onUpgrade error");
		}
		onCreate(db); 
	} 
	
	//============= 已验票表操作 ==============
	/**
	 * 创建最高分表操作 
	 */
	public boolean creatMaxScoreInfo(SQLiteDatabase db) {
		// 名字	手机号	日期		时间
		String sql = "CREATE TABLE " + TABLE_NAME + " (" + SCORE_SOUND_OPEN + " text, " + SCORE_MAX +" text);"; 
		try{
			db.execSQL(sql);
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"creatMaxScoreInfo error");
			return false;
		}
		return true;
	}
	
	/**
	 * 增加已验票信息
	 * 
	 */
	public boolean insertMaxScore(int scoreMax, boolean openSound) { 
		if(SDHelper.getSDPath() == null)
			return false;
		SQLiteDatabase db = openDBByName(DATABASE_NAME);
		if(db == null)
			return false;
		// 如果表不存在
		if(!foundTableByName(TABLE_NAME)){
			creatMaxScoreInfo(db);
		} 
		deleteTableAllDataByName(TABLE_NAME);
		ContentValues cv = new ContentValues(); 
		cv.put(SCORE_SOUND_OPEN, openSound ? "1" : "0"); 
		cv.put(SCORE_MAX, String.valueOf(scoreMax)); 
		try{
			db.insert(TABLE_NAME, null, cv); 
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"insertMaxScore error");
			return false;
		}
		return true;
	}
	
	/**
	 * 查询卖票备注
	 * @return 查询结果
	 */
	public int[] queryMaxScore() { 
		if(SDHelper.getSDPath() == null)
			return null;
		SQLiteDatabase db = openDBByName(DATABASE_NAME);
		if(db == null)
			return null;
		Cursor cursor = null;
		int score[] = new int[2];
		try{
			cursor = db.query(TABLE_NAME, null, null, null, null, null, null); 
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"queryMaxScore error");
			return null;
		}
		if(cursor != null && cursor.moveToFirst()){
			score[0] = Integer.valueOf(cursor.getString(0));
			score[1] = Integer.valueOf(cursor.getString(1));
			return score;
		}
		return null;
	}

	//============= 数据库操作 ==============
	/**
	* 删除数据库
	* 
	* @DBName 数据库名字
	*/
	public boolean deleteDBByName(String DBName) {
		try{
			context.deleteDatabase(DBName);
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"deleteDBByName error");
			return false;
		}
		return true;
	}

	/**
	 * 判断表是否存在
	 * 
	 * @DBName 数据库表名字
	 */
	public boolean foundTableByName(String tableName) {
//		SQLiteDatabase db = this.getWritableDatabase(); 
		SQLiteDatabase db = openDBByName(DATABASE_NAME);
		if(db == null)
			return false;
		Cursor cursor = null;
		try{
			cursor = db.query(tableName, null, null, null, null, null, null); 
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"foundTableByName error");
			return false;
		}
		if(cursor == null)
			return false;
		return true;
	}

	/**
	 * 删除数据库的表
	 * 
	 * @DBName 数据库表名字
	 */
	public boolean deleteTableByName(String tableName) {
		SQLiteDatabase db = openDBByName(DATABASE_NAME);
		if(db == null)
			return false;
		String sql="drop table " + tableName;
		try{
			db.execSQL(sql);
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"deleteTableByName error");
			return false;
		}
		return true;
	}
	
	/**
	 * 删除数据库的表内容
	 * 
	 * @DBName 数据库表名字
	 */
	public boolean deleteTableAllDataByName(String tableName) {
		SQLiteDatabase db = openDBByName(DATABASE_NAME);
		if(db == null)
			return false;
		try{
			db.delete(tableName, null, null);
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"deleteTableAllDataByName error");
			return false;
		}
		return true;
	}
	
	/**
	* 打开数据库
	* @DBName 数据库名字
	*/
	public SQLiteDatabase openDBByName(String DBName) {
		if(DBName == null)
			return null;
		SQLiteDatabase db = null;
		try{
			db = context.openOrCreateDatabase(DBName, Context.MODE_PRIVATE, null);
		}catch (Exception e) {
			Log.i(AppConfig.Tag.DB,"openDBByName error");
			return null;
		}
		return db;
	}

}